#define _CRT_SECURE_NO_WARNINGS 1

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
 
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode* sHead = new ListNode(0);
        ListNode* sTail = sHead;
        ListNode* bHead = new ListNode(0);
        ListNode* bTail = bHead;
        ListNode* cur = head;
        while (cur) {
            if (cur->val < x) {
                sTail->next = cur;
                sTail = sTail->next;
            }
            else {
                bTail->next = cur;
                bTail = bTail->next;
            }
            cur = cur->next;
        }
        sTail->next = bHead->next;
        bTail->next = nullptr;
        ListNode* ret = sHead->next;
        delete sHead;
        delete bHead;
        return ret;
    }
};

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* cur1 = l1, * cur2 = l2;
        int carry = 0;
        ListNode* head = new ListNode(0);
        ListNode* tail = head;
        while (cur1 || cur2 || carry) {
            int sum = carry;
            if (cur1) {
                sum += cur1->val;
                cur1 = cur1->next;
            }
            if (cur2) {
                sum += cur2->val;
                cur2 = cur2->next;
            }
            tail->next = new ListNode(sum % 10);
            tail = tail->next;
            carry = sum / 10;
        }
        ListNode* ret = head->next;
        delete head;
        return ret;
    }
};